﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Xml;

namespace StringFileComparetor
{
    public partial class Form1 : Form
    {
        const String DATA_SET = "dataset";
        const String DATA_TABLE = "datatable";
        const string STRING_TAG = "string";

        DataSet mDataSet=null;
        
        OpenFileDialog openFileDialog;
        
        String inputFile1, inputFile2;
        List<String> strings1, strings2,only1;
        Boolean reportFirst;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = true;
            //tbInput1.Text = @"C:\Users\Long\Desktop\strings.xml";
            //tbInput2.Text = @"C:\Users\Long\Desktop\strings2.xml";


        }

        private void btnOpen1_Click(object sender, EventArgs e)
        {
            inputFile1 = getInputXmlFile();
            if (!inputFile1.Equals(""))
            {
                tbInput1.Text = inputFile1;
            }
        }

        private void btnOpen2_Click(object sender, EventArgs e)
        {
            inputFile2 = getInputXmlFile();
            if (!inputFile2.Equals(""))
            {
                tbInput2.Text = inputFile2;
            }

        }

        private void btnCompare_Click(object sender, EventArgs e)
        {

            do
            {
                //Hide file count
                lblFile1Count.Visible = false;
                lblFile2Count.Visible = false;

                reportFirst = false;
                dataGridView1.DataSource = null;


                inputFile1 = tbInput1.Text;
                //Check file Exists
                if (!File.Exists(inputFile1))
                {
                    MessageBox.Show("File 1 not exists");
                    break;
                }

                inputFile2 = tbInput2.Text;
                if (!File.Exists(inputFile2))
                {
                    MessageBox.Show("File 2 not exists");
                    break;
                }

                backgroundWorker1.RunWorkerAsync();

          } while (false);
     }
          


        private string getInputXmlFile()
        {
            string input = "";

            if (openFileDialog == null)
            {
                openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                openFileDialog.FilterIndex = 1;
                openFileDialog.RestoreDirectory = true;

            }
            else
            {
                String file = openFileDialog.FileName;
                openFileDialog.InitialDirectory = file;
           
            }
            
            DialogResult result = openFileDialog.ShowDialog();

            switch (result)
            {
                case DialogResult.OK:
                    input = openFileDialog.FileName;
                    break;
                case DialogResult.Cancel:
                    break;
            }
            return input;

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            
           
           
            //Get strings in file 1
            strings1 = getStringNameInXml(inputFile1, STRING_TAG);
            backgroundWorker1.ReportProgress(strings1.Count);
           
           //Get string file 2
            strings2 = getStringNameInXml(inputFile2, STRING_TAG);
            backgroundWorker1.ReportProgress(strings2.Count);
           only1 = new List<string>();

           foreach (String s1 in strings1)
            {
                if (strings2.Contains(s1))
                {
                    strings2.Remove(s1);
                }
                else
                {
                    only1.Add(s1);
                }
            }


            //Add to data set

           List<ListViewItem> listviewItems = new List<ListViewItem>();

           // Create a DataSet.
           mDataSet = new DataSet(DATA_SET);

           // Create two DataTables.
           DataTable tCust = new DataTable(DATA_TABLE);

           String file1 = Path.GetFileName(inputFile1);
           String file2 = Path.GetFileName(inputFile2);

           //Add new column
   
           tCust.Columns.Add("File1: "+file1,typeof(String));
           tCust.Columns.Add("File2: "+file2, typeof(String));
           
           // Add the tables to the DataSet.
           mDataSet.Tables.Add(tCust);

           String[] row = new String[2];

           foreach (String s in only1)
           {
               row[0] = s;
               row[1] = "";
               tCust.Rows.Add(row);
           }

           foreach (String s in strings2)
           {
               row[0] = "";
               row[1] = s;
               tCust.Rows.Add(row);
           }


        }



        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (reportFirst==false)
            {
                lblFile1Count.Text= e.ProgressPercentage.ToString()+" strings";
                reportFirst = true;
                lblFile1Count.Visible = true;
                return;
            }
            lblFile2Count.Text = e.ProgressPercentage.ToString() + " strings";
            lblFile2Count.Visible = true;

        }


        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dataGridView1.DataSource = mDataSet; // dataset
            dataGridView1.DataMember = DATA_TABLE; // table name you need to show
            MessageBox.Show("Done!!!");

        }




        List<string> getStringNameInXml(String xmlFilePath, String tagName)
        {
            List<string> strings = new List<string>();
            do 
            {
                if (File.Exists(xmlFilePath)==false)
                {
                    break;
                }

                XmlDocument doc = new XmlDocument();
                doc.Load(xmlFilePath);
                XmlNodeList nodeList = doc.GetElementsByTagName(tagName);
                if (nodeList == null)
                {
                    break;
                }
                String s;
                foreach (XmlNode node in nodeList)
                {
                    s=node.Attributes["name"].Value;
                    strings.Add(s);
                }

            
            } while (false);
        
            return strings;
        
        }

      

            

    }
}
